package com.wfy.common.util;

import com.wfy.common.exception.MyException;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 文件操作工具类，内部使用apache io包
 */
public class FileUtil {

    public static void main(String[] args) throws IOException {

    }

    /**
     * 创建目录
     *
     * @param destDirName 目标目录名
     * @return 目录创建成功返回true，否则返回false
     */
    public static boolean createDir(String destDirName) throws MyException {
        try {
            File file = new File(destDirName);
            FileUtils.forceMkdir(file);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    /**
     * 删除文件
     *
     * @param filePathAndName
     */
    public static void delFile(String filePathAndName) {
        try {
            FileUtils.forceDelete(new File(filePathAndName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取到字节数组0
     *
     * @param filePath //路径
     * @throws IOException
     */
    public static byte[] getContent(String filePath) throws MyException {
        File file = new File(filePath);
        long fileSize = file.length();
        if (fileSize > Integer.MAX_VALUE) {
            System.out.println("file too big...");
            return null;
        }
        try (FileInputStream fi = new FileInputStream(file)) {
            byte[] buffer = new byte[(int) fileSize];
            int offset = 0;
            int numRead = 0;
            while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
                offset += numRead;
            }
            // 确保所有数据均被读取
            if (offset != buffer.length) {

            }
            return buffer;
        } catch (IOException e) {
            throw new RuntimeException("Could not completely read file " + file.getName());

        }
    }

    /**
     * 读取到字节数组1
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(String filePath) throws MyException {
        File f = new File(filePath);
        try {
            return FileUtils.readFileToByteArray(f);
        } catch (IOException e) {
            throw new RuntimeException("读取错误");
        }
    }

    /**
     * 读取到字节数组2
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray2(String filePath) throws MyException {
        File f = new File(filePath);
        if (!f.exists()) {
            throw new RuntimeException("文件不存在");
        }
        try {
            return FileUtils.readFileToByteArray(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Mapped File way MappedByteBuffer 可以在处理大文件时，提升性能
     *
     * @param
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray3(String filePath) throws MyException {
        File f = new File(filePath);
        try {
            return FileUtils.readFileToByteArray(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}